const TemplatesCategoryModel = require('../../models/flexiPage/templatesCategoryModel')

const createCategory = async (ctx) => {
  const { name, parentId, sortOrder } = ctx.request.body
  try {
    const result = await TemplatesCategoryModel.create({ name, parentId, sortOrder })
    ctx.status = 201
    ctx.body = { success: true, data: result }
  } catch (error) {
    ctx.status = 400
    ctx.body = { success: false, message: error.message }
  }
}

const getAllCategories = async (ctx) => {
  const categories = await TemplatesCategoryModel.findAll()
  ctx.body = { success: true, data: categories }
}

const deleteCategory = async (ctx) => {
  const { id } = ctx.params
  try {
    await TemplatesCategoryModel.deleteWithChildren(id)
    ctx.status = 204
  } catch (error) {
    ctx.status = 400
    ctx.body = { success: false, message: error.message }
  }
}

module.exports = {
  createCategory,
  getAllCategories,
  deleteCategory,
}